package utils;

/**
 * Created by Administrator on 2017/5/20.
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import constants.Constants;


public class ActivityUtil {

    public static void jumpToActivity(Activity mContext, Class<? extends Activity> targetActivity) {
        Intent intent = new Intent(mContext, targetActivity);
        mContext.startActivity(intent);
    }

    //list2中有,list1中没有         6    5
    public static List<String> getDifferent(List<String> list1, List<String> list2) {
        List<String> diff = new ArrayList<String>();
        Map<String, Integer> map = new HashMap<String, Integer>(list1.size());

        for (String string : list1) {
            map.put(string, 1);
        }

        for (String string : list2) {
            if (map.get(string) != null) {
                continue;
            }
            diff.add(string);
        }
        return diff;
    }


    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.reset();

            md.update(str.getBytes("UTF-8"));
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
//            String s = new BigInteger(1, md.digest()).toString(16);
//            return s.toUpperCase();
            byte[] byteArray = md.digest();

            StringBuffer md5StrBuff = new StringBuffer();

            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                } else {
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
                }
            }
            //16位加密，从第9位到25位
            return md5StrBuff.toString().toUpperCase();
        } catch (Exception e) {
        }

        return null;
    }

    public static SharedPreferences getSharedPreferences(Context mContext) {
        return mContext.getSharedPreferences(Constants.SHARED_PREFERENCE, Context.MODE_PRIVATE);
    }

}